/*
 * @(#) $Header$
 *
 * Copyright (C) 2006  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.swing.event;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import ca.forklabs.baselib.swing.event.AbstractSwitchListener;

/**
 * Class {@code SwitchActionListener} holds action listeners with only one
 * considered active. The event is always forwarded to the active listener. The
 * default listener is {@link #DO_NOTHING_ACTION_LISTENER}.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.swing.event.SwitchActionListener">Daniel Léonard</a>
 * @version $Revision$
 */
public class SwitchActionListener extends AbstractSwitchListener<ActionListener> implements ActionListener {

//---------------------------
// Class variables
//---------------------------

   /**
    * The do-nothing action listener.
    */
   public static final ActionListener DO_NOTHING_ACTION_LISTENER = new ActionListener() {
      /**
       * Does nothing.
       * @param   ae   ignored.
       */
      @Override
      public void actionPerformed(ActionEvent ae) {
      // nothing
         }
      };


//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    */
   public SwitchActionListener() {
   // nothing
      }


//---------------------------
// Implemented methods from ca.forklabs.baselib.swing.event.AbstractSwitchListener
//---------------------------

   /**
    * Gets the default listener for the switch.
    * @return   the listener.
    */
   @Override
   protected ActionListener getDefaultListener() {
      ActionListener default_listener = SwitchActionListener.DO_NOTHING_ACTION_LISTENER;
      return default_listener;
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Adds a new action listener to the switch.
    * @param   key    the key of the listener.
    * @param   listener   the listener.
    */
   public void addActionListener(Object key, ActionListener listener) {
      this.addListener(key, listener);
      }

   /**
    * Removes an action listener from the switch.
    * @param   key    the key of the listener.
    */
   public void removeActionListener(Object key) {
      this.removeListener(key);
      }


//---------------------------
// Implemented methods from java.awt.event.ActionListener
//---------------------------

   /**
    * Forwards the event to the active listener.
    * @param   ae   the action event.
    */
   @Override
   public void actionPerformed(ActionEvent ae) {
      ActionListener listener = this.getActiveListener();
      listener.actionPerformed(ae);
      }

   }
